package org.jeecg.modules.flower.util;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.modules.flower.entity.FlowerPlantingGreenhouse;
import org.jeecg.modules.flower.entity.FlowerStorehouse;
import org.jeecg.modules.flower.service.IFlowerPlantingGreenhouseService;
import org.jeecg.modules.flower.service.IFlowerStorehouseService;
import org.jeecg.modules.system.service.ISysDictService;

import java.io.IOException;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CommonUtil {

    /**
     *
     * 入库、出库单 、、预出库锁定关键字
     * @descriptionauthor
     * @author gf
     * 2023-3-1 19:34
     *
     * @return
     */
    public final static String OutOrderLockKey = "flowerHJPOutOrderLockKey" ;
    /**
     * 锁定库存台账的锁关键字
     *
     * @descriptionauthor
     * @author gf
     * 2022-12-28 10:13
     * @return
     */
    public static final String SUBTAGINFOKEY = "subTagInfoLockKeyVagetable001";//锁定库存台账的锁关键字
    /**
     * 锁定 预出库库存台账的锁关键字
     *
     * @descriptionauthor
     * @author gf
     * 2022-12-28 10:13
     * @return
     */
    public static final String SUBTAGPREINFOKEY = "subTagPreInfoLockKeyVagetable002";//锁定预出库库存台账的锁关键字

    /**
     * 自动生成出库单号
     *
     * @param pre   前缀
     * @param oldNo 旧单号
     * @param leng  流水号长度
     * @return
     */
    public static String getNo(String pre, String oldNo, Integer leng) {
        //自动生成出库单号
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        String dateNo = simpleDateFormat.format(new Date());
        String no = pre + dateNo;
        Integer n = 1;
        if (StringUtils.isNotBlank(oldNo) && oldNo.length() > leng) {
            String oldNoNum = oldNo.substring(oldNo.length() - leng, oldNo.length());
            n = Integer.parseInt(oldNoNum) + 1;
        }
        String s = subLength(n, leng);
        no = no + s;
        return no;
    }

    /**
     * 自动生成出库单号--不带日期的
     *
     * @param pre   前缀
     * @param oldNo 旧单号
     * @param leng  流水号长度
     * @return
     */
    public static String getNoNoDate(String pre, String oldNo, Integer leng) {
        //自动生成出库单号
        String no = pre;
        Integer n = 1;
        if (StringUtils.isNotBlank(oldNo) && oldNo.length() > leng) {
            String oldNoNum = oldNo.substring(oldNo.length() - leng, oldNo.length());
            n = Integer.parseInt(oldNoNum) + 1;
        }
        String s = subLength(n, leng);
        no = no + s;
        return no;
    }

    /**
     * 保留位数
     *
     * @return java.lang.String
     * @descriptionauthor
     * @author gf
     * 2022-9-16 10:52
     * [pre, oldNo, leng]
     */
    public static String subLength(Integer number, Integer leng) {
        //自动生成出库单号
        NumberFormat formatter = NumberFormat.getNumberInstance();
        formatter.setMinimumIntegerDigits(leng);
        formatter.setGroupingUsed(false);
        String s = formatter.format(number);
        return s;
    }


    /**
     * 发送post请求
     *
     * @return com.alibaba.fastjson.JSONObject
     * @descriptionauthor
     * @author gf
     * 2022-9-22 9:48
     * [url, param, token]
     */
    public static JSONObject sendPost(String url, JSONObject param, String token) {
        //定义接收数据
        JSONObject result = new JSONObject();
        HttpPost httpPost = new HttpPost(url);
        CloseableHttpClient client = HttpClients.createDefault();
        //请求参数转JOSN字符串
        StringEntity entity = new StringEntity(param.toJSONString(), "UTF-8");
        entity.setContentEncoding("UTF-8");
        entity.setContentType("application/json");
        httpPost.setEntity(entity);
        httpPost.setHeader("X-Access-Token", token);
        try {
            HttpResponse response = client.execute(httpPost);
            if (response.getStatusLine().getStatusCode() == 200) {
                result = JSONObject.parseObject(EntityUtils.toString(response.getEntity(), "UTF-8"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put("error", "连接错误！" + e.getMessage());
        }
        return result;
    }

    /**
     * 发送get请求
     *
     * @return com.alibaba.fastjson.JSONObject
     * @descriptionauthor
     * @author gf
     * 2022-9-22 9:48
     * [url, token]
     */
    public static JSONObject sendGet(String url, String token) throws IOException {
        JSONObject result = new JSONObject();
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(url);
        //添加请求头
        request.addHeader("X-Access-Token", token);
        HttpResponse response = client.execute(request);
        try {
            // 获取返回的数据
            if (response.getStatusLine().getStatusCode() == 200) {
                result = JSONObject.parseObject(EntityUtils.toString(response.getEntity(), "UTF-8"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put("error", "连接错误！" + e.getMessage());
        }
        return result;
    }

    public static Map<String, String> getHouseNames() {
        IFlowerStorehouseService flowerStorehouseService = SpringContextUtils.getApplicationContext().getBean(IFlowerStorehouseService.class);
        QueryWrapper<FlowerStorehouse> queryWrapperHouse = new QueryWrapper<>();
        List<FlowerStorehouse> flowerStorehousesList = flowerStorehouseService.list(queryWrapperHouse);
        Map<String, String> housemap = new HashMap<>();
        for (FlowerStorehouse flowerStorehouse : flowerStorehousesList) {
            housemap.put(flowerStorehouse.getCode(), flowerStorehouse.getName());
        }
        return housemap;
    }

    public static Map<String, String> getCostNames() {
        ISysDictService sysDictService = SpringContextUtils.getApplicationContext().getBean(ISysDictService.class);

        List<DictModel> ch_cost = sysDictService.getDictItems("ch_cost");
        Map<String, String> costMap = new HashMap<>();
        for (DictModel cost : ch_cost) {
            costMap.put(cost.getValue(), cost.getText());
        }
        return costMap;
    }

    //缓存大棚名字
    public static Map<String, String> getGreenHouseNames() {
        IFlowerPlantingGreenhouseService flowerPlantingGreenhouseService = SpringContextUtils.getApplicationContext().getBean(IFlowerPlantingGreenhouseService.class);
        QueryWrapper<FlowerPlantingGreenhouse> queryWrapperHouse = new QueryWrapper<>();
        List<FlowerPlantingGreenhouse> flowerStorehousesList = flowerPlantingGreenhouseService.list(queryWrapperHouse);
        Map<String, String> map = new HashMap<>();
        for (FlowerPlantingGreenhouse flowerStorehouse : flowerStorehousesList) {
            map.put(flowerStorehouse.getId(), flowerStorehouse.getName());
        }
        return map;
    }
    /**
     *
     * 根据 库房 获取 成本中心
     * @descriptionauthor
     * @author gf
     * 2023-3-1 15:34
     * []
     * @return java.util.Map<java.lang.String, java.lang.String>
     */
    public static Map<String,String> getCostByStorehouse()  {
        IFlowerStorehouseService  flowerStorehouseService = SpringContextUtils.getApplicationContext().getBean(IFlowerStorehouseService.class);
        QueryWrapper<FlowerStorehouse> queryWrapperHouse =new QueryWrapper<>();
        List<FlowerStorehouse> flowerStorehousesList = flowerStorehouseService.list(queryWrapperHouse) ;
        Map<String,String> housemap =new HashMap<>();
        for(FlowerStorehouse flowerStorehouse : flowerStorehousesList){
            housemap.put(flowerStorehouse.getCode(), flowerStorehouse.getCost());
        }
        return housemap;
    }
    /**
     * 是否和销售系统强制关联
     *
     * @return java.lang.Boolean
     * @descriptionauthor
     * @author gf
     * 2023-3-24 13:48
     * []
     */
    public static Boolean isLinkSealSystem() {
        Boolean isLink = false;
        ISysDictService sysDictService = SpringContextUtils.getApplicationContext().getBean(ISysDictService.class);
        List<DictModel> ch_isLinkSealerSystem = sysDictService.getDictItems("ch_isLinkSealerSystem");
        if (ch_isLinkSealerSystem.size() > 0) {
            DictModel dictModel = ch_isLinkSealerSystem.get(0);
            String text = dictModel.getText();
            if ("是".equals(text)) {
                isLink = true;
            }
        }
        return isLink;
    }

}
